/*
 * ksBlockEntry.cpp
 *
 *  Created on: Aug 3, 2011
 *      Author: wang
 */

#include "ksBlockEntry.h"
#include <iostream>
#include <cstring>
#include <fstream>
#include "ksBlock.h"
#include "ksException.h"

ksBlockEntry::ksBlockEntry()
{
}

ksBlockEntry::~ksBlockEntry()
{
}

int ksBlockEntry::load(unsigned char *data, unsigned int length)
{
    unsigned int copyLength = sizeof(BlockEntry);
    if (copyLength > length)
    {
        throw ksException("overflow");
        return 0;
    }
    memcpy(&_blockEntry, data, copyLength);
    //_blockEntry.type[kBlockTypeMaxLength - 1] = 0x00;
    //_blockEntry.id[kBlockIDMaxLength - 1] = 0x00;

    LogObj("");
    description();

    return copyLength;
}

void ksBlockEntry::description()
{
    std::cout << "blockEntry:{ ";
    std::cout << "type:" << blockType2String(_blockEntry.type);
    std::cout << ",id:" << _blockEntry.id;
    std::cout << ",offset:" << _blockEntry.offset;
    std::cout << ",length:" << _blockEntry.length;
    std::cout << " }" << std::endl;
}

void ksBlockEntry::save(std::ofstream& out)
{
    unsigned int copyLength = sizeof(BlockEntry);
    out.write((char*) &_blockEntry, copyLength);
}

void ksBlockEntry::setId(unsigned short id )
{
    //safe_strcopy(_blockEntry.id, id, kBlockIDMaxLength);
    _blockEntry.id = id;
}

void ksBlockEntry::setType(unsigned short type )
{
    //safe_strcopy(_blockEntry.type, type, kBlockTypeMaxLength);
    _blockEntry.type = type;
}

void ksBlockEntry::setOffset(unsigned int offset)
{
    _blockEntry.offset = offset;
}

void ksBlockEntry::setLength(unsigned int length)
{
    _blockEntry.length = length;
}

unsigned short ksBlockEntry::type()const
{
    return _blockEntry.type;
}

unsigned short ksBlockEntry::id() const
{
    return _blockEntry.id;
}

unsigned int ksBlockEntry::offset()const
{
    return _blockEntry.offset;
}

unsigned int ksBlockEntry::length()const
{
    return _blockEntry.length;
}
